<script setup>
import { inject, ref } from "vue";

import { upgradelist, upgradeallnew, upgradedetails } from "@/services/upfrade";
import { useCounterStore } from "@/stores/upgrade";
import { userinfo } from "@/services/user";
import { onLoad } from "@dcloudio/uni-app";

const isgeive = ref(false);

onLoad((e) => {
  if (e.give) {
    isgeive.value = true;
  }
});

const dateobj = useCounterStore();
let list = ref([]);
const userlive = ref("");
getlistfun();
upgradeallnewfun();
async function getlistfun() {
  let res = await upgradelist();
  list.value = res.data;
}

async function userinfofun() {
  let res = await userinfo();
}

const upgradeshow = ref(false);
const upgradeshowtwo = ref(false);
const upgradestype = ref(0);
const upgradesid = ref("");
const itemobj = ref([]);
const newlevelid = ref("");

async function upgradeallnewfun() {
  let resss = await userinfo();

  if (resss.code == 200) {
    userlive.value = resss.data.level_id;
  }
  let res = await upgradeallnew();
  console.log(Array.isArray(res.data.upgrade_id));

  if (Array.isArray(res.data.upgrade_id)) {
    newlevelid.value = resss.data.level_id + 1;
    return;
  }

  if (res.data.agree_id > 0) {
    upgradestype.value = 2;
    upgradesid.value = res.data.agree_id;

    if (isgeive.value) {
      goupgrade();
      return;
    }
    upgradeshow.value = true;
    // uni.navigateTo({
    //   url:'/pages/upgrade/identityform?type=1&agree_id='+res.data.agree_id
    // })
    return;
  }
  if (res.data.upgrade_id && res.data.upgrade_id.id > 0) {
    upgradestype.value = 1;
    upgradesid.value = res.data.upgrade_id.id;
    newlevelid.value = res.data.upgrade_id.level_id;
    if (isgeive.value) {
      goupgrade();
      return;
    }
    upgradeshow.value = true;
    // uni.navigateTo({
    //   url:'/pages/upgrade/identityform?type=1&upgrade_id='+res.data.upgrade_id
    // })
  }
}

const gopayupgrade = async (item) => {
  itemobj.value = item;
  console.log(item);
  // userlive

  console.log(userlive.value);
  if (userlive.value >= item.id || item.id <= newlevelid.value) {
    uni.showToast({
      title: "您已拥有该身份或大于该身份",
      icon: "none",
    });
    return;
  }
  if (!item && userlive.value < 301) {
    uni.showToast({
      title: "经销商及以上身份可升级",
      icon: "none",
    });
    return;
  }
  let res2 = await upgradeallnew();
  // if (
  //   res2.data.upgrade_id &&
  //   res2.data.upgrade_id.level_id &&
  //   res2.data.upgrade_id.level_id < userlive.value
  // ) {
  //   uni.showToast({
  //     title: "您已拥有该身份或大于该身份",
  //     icon: "none",
  //   });
  //   return;
  // }

  if (
    res2.data.upgrade_id &&
    res2.data.upgrade_id.level_id &&
    res2.data.upgrade_id.level_id == item.id
  ) {
    upgradeshow.value = true;
    return;
  }

  if (res2.data.agree_id > 0) {
    upgradeshowtwo.value = true;
    // uni.navigateTo({
    //   url:'/pages/upgrade/identityform?type=1&agree_id='+res.data.agree_id
    // })
    return;
  }

  if (res2.data.upgrade_id && res2.data.upgrade_id.id > 0) {
    upgradeshowtwo.value = true;
    // uni.navigateTo({
    //   url:'/pages/upgrade/identityform?type=1&upgrade_id='+res.data.upgrade_id
    // })
    return;
  }

  dateobj.formdate = {};
  if (itemobj.value) {
    uni.navigateTo({
      url:
        "/pages/upgrade/identityform?id=" +
        itemobj.value.id +
        "&product_id=" +
        itemobj.value.product_id,
    });
  } else {
    uni.navigateTo({
      url: "/pages/upgrade/identityform?add=1",
    });
  }
};

const goupaysffun = () => {
  dateobj.formdate = {};
  if (itemobj.value) {
    uni.navigateTo({
      url:
        "/pages/upgrade/identityform?id=" +
        itemobj.value.id +
        "&product_id=" +
        itemobj.value.product_id,
    });
  } else {
    uni.navigateTo({
      url: "/pages/upgrade/identityform?add=1",
    });
  }
};

// const soleAgency = () => {

//   uni.navigateTo({
//     url: "/pages/upgrade/identityform"
//   });
// };

//升级弹框

const close = () => {
  upgradeshow.value = false;
};

const goupgrade = async () => {
  // uni.navigateTo({
  //   url: "/pages/upgrade/identityform?type=1&upgrade_id=" + upgradesid.value,
  // });
  // return;

  try {
    let res = await upgradedetails({
      type: upgradestype.value == 1 ? 1 : 2,
      id: upgradestype.value == 1 ? upgradesid.value : upgradesid.value,
    });

    upgradeshow.value = false;
    if (upgradestype.value == 1) {
      // uni.navigateTo({
      //   url:
      //     "/pages/upgrade/identityform?type=1&upgrade_id=" + upgradesid.value,
      // });
      // return;

      if (res.data.data_status == 0) {
        uni.navigateTo({
          url:
            "/pages/upgrade/identityform?type=1&upgrade_id=" + upgradesid.value,
        });
        return;
      }
      if (res.data.agree_status == 0) {
        if (res.data.pay_status == 0) {
          uni.navigateTo({
            url:
              "/pages/upgrade/agreement?type=1" +
              "&product_id=" +
              "&id=" +
              upgradesid.value +
              "&level_id=" +
              res.data.level_id +
              "&pay_status=",
          });
        } else {
          uni.navigateTo({
            url:
              "/pages/upgrade/agreement?type=1" +
              "&product_id=" +
              "&id=" +
              upgradesid.value +
              "&level_id=" +
              res.data.level_id +
              "&pay_status=1",
          });
        }

        return;
      }
      if (res.data.pay_status == 0) {
        uni.navigateTo({
          url:
            "/pages/upgrade/upgradepay?level_id=&id=" +
            upgradesid.value +
            "&product_id=&type=1",
        });
        return;
      }
    }
    if (upgradestype.value == 2) {
      // uni.navigateTo({
      //   url: "/pages/upgrade/identityform?type=1&agree_id=" + upgradesid.value,
      // });

      // debugger;

      if (res.data.data_status == 0) {
        uni.navigateTo({
          url:
            "/pages/upgrade/identityform?type=1&agree_id=" + upgradesid.value,
        });
        return;
      }
      if (res.data.agree_status == 0) {
        if (res.data.pay_status == 0) {
          uni.navigateTo({
            url:
              "/pages/upgrade/agreement?type=2" +
              "&product_id=" +
              "&id=" +
              upgradesid.value +
              "&level_id=&pay_status=",
          });
        } else {
          uni.navigateTo({
            url:
              "/pages/upgrade/agreement?type=2" +
              "&product_id=" +
              "&id=" +
              upgradesid.value +
              "&level_id=&pay_status=1",
          });
        }

        return;
      }
      if (res.data.pay_status == 0) {
        uni.navigateTo({
          url:
            "/pages/upgrade/upgradepay?level_id=&id=" +
            upgradesid.value +
            "&product_id=&type=2",
        });
        return;
      }
    }
  } catch (e) {
    uni.showToast({
      title: e.info,
      icon: "none",
    });
  }
};
const closetow = () => {
  upgradeshowtwo.value = false;
};
</script>

<template>
  <view class="upgrade">
    <view class="upgrade-top">
      <image src="@/pages/upgrade/static/home-bg.png" mode="widthFix" />
    </view>

    <view class="list">
      <view class="list-top">
        <view class="list-top-title">请选择身份购买</view>
        <image src="@/pages/upgrade/static/titletop.png" mode="scaleToFill" />
      </view>
      <view class="list-card" v-for="(item, index) in list" :key="index">
        <view class="list-card-top">
          <view class="list-card-top-img">
            <!-- <image src="./static/img-1.png" mode="widthFix"/></view> -->
            <image :src="item.product.image" mode=""
          /></view>

          <view class="list-card-right">
            <text class="text1">(成为{{ item.name }})</text>
            <text class="text2">{{ item.product.store_name }}</text>
            <text class="text3">¥ {{ item.vip_price }}元</text>
          </view>
        </view>
        <view
          class="list-card-btn"
          :class="
            item.id <= userlive || item.id <= newlevelid
              ? 'list-card-btn-color2'
              : ''
          "
          @click="gopayupgrade(item)"
          >立即购买</view
        >
      </view>
      <!-- <view class="list-card">
        <view class="list-card-top">
          <view class="list-card-top-img"
            ><image src="./static/img-2.png" mode="widthFix"
          /></view>
          <view class="list-card-right">
            <text class="text1">(成为经销商)</text>
            <text class="text2">经销商升级大礼包</text>
            <text class="text3">¥ 20,000元</text>
          </view>
        </view>
        <view class="list-card-btn">立即购买</view>
      </view> -->
    </view>
  </view>

  <view class="password_show">
    <up-popup
      :show="upgradeshow"
      mode="center"
      round="10"
      @close="close"
      @open="open"
    >
      <view class="popup-content">
        <view class="popup-content-title">升级合同</view>
        <view class="popup-content-dec"
          >您有身份升级合同未完善， 请完善信息及签订合同</view
        >
        <view class="popup-content-bt">
          <view class="popup-content-qx" @click="upgradeshow = false"
            >取消</view
          >
          <view class="popup-content-cl" @click="goupgrade">立即处理</view>
        </view>
      </view>
    </up-popup>
  </view>

  <view class="password_show">
    <up-popup
      :show="upgradeshowtwo"
      mode="center"
      round="10"
      @close="closetow"
      @open="open"
    >
      <view class="popup-content">
        <view class="popup-content-title">提示</view>
        <view class="popup-content-dec"
          >您已有升级未完善，可完成合同签订或重新购买</view
        >
        <view class="popup-content-bt">
          <view class="popup-content-qx" @click="goupaysffun">去购买身份</view>
          <view class="popup-content-cl" @click="goupgrade">去填写</view>
        </view>
      </view>
    </up-popup>
  </view>
</template>

<style lang="scss" scoped>
@import "./identitylv.scss";
</style>
